<template>
  <div class="shopinfo">
    <div class="slot">
      <div class="top">
        <div class="return" @click="goback">
          <img src="@/assets/my/zuo.png" alt />
        </div>
        <span>商品详情</span>
      </div>
    </div>
    <div class="swiper" ref="element">
      <MySwiper swipeid="swipe" ref="swiper" :autoPlay="3000" effect="slide">
        <div
          class="swiper-slide"
          slot="swiper-con"
          v-for="(item, index) in shopinfo.images"
          :key="index"
        >
          <img @click="tupianyulan(shopinfo.images)" :src="item" alt />
        </div>
      </MySwiper>
    </div>
    <div class="info">
      <div class="name">{{ shopinfo.title }}</div>
      <div class="more">
        <div class="price">
          <span class="span1">
            ￥
            <span class="span2">{{ shopinfo.o_price }}</span>
          </span>
        </div>
        <div class="num">
          最低销售价：￥{{ shopinfo.l_price }}/{{ shopinfo.unit }}
        </div>
      </div>
    </div>
    <div class="xiangq">———— 商品详情 ————</div>
    <div class="xqqqqqq" v-html="shopcontent"></div>
    <div style="height: 70px"></div>
    <div class="bottom">
      <!-- <div class="yixuan">
        <div >
          已选：
          <span class="span1"
            >{{ specs.length === 0 ? shopinfo.title : specsinfo.name }}
            {{ btcount === "" ? 0 : btcount }}{{ shopinfo.unit }}</span
          >
        </div>
      </div> -->
      <div class="btn" @click="$router.push('/cart')">返回购物车</div>
      <div class="btn" @click="guigecaigou">加入购物车</div>
    </div>
    <!-- 选规格 -->
    <div class="zhezhao" v-show="isguige"></div>
    <div class="zhezhao2" v-show="isguige">
      <div class="tankuang2">
        <div class="top">
          <div class="name">{{ shopinfo.title }}</div>
          <div class="guanbi" @click="guanbi">
            <img src="@/assets/home/cha.png" alt />
          </div>
        </div>
        <div class="guige" v-show="specs.length !== 0">
          <div class="p4">规格</div>
          <div class="ggbtn">
            <div
              :class="active === item.id ? 'btn1' : 'btn2'"
              @click.stop="xuanguige(item.id)"
              v-for="item in specs"
              :key="item.id"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="guige">
          <div class="p4">数量</div>
          <div class="num">
            <ul class="shuliangtianxie">
              <li class="num1">
                <div class="imgsa">
                  <img
                    src="@/assets/my/sub.png"
                    @click.stop="subtract()"
                    class="subaddbt"
                  />
                </div>
              </li>

              <li class="num1">
                <input
                  class="input"
                  @change="change($event)"
                  placeholder="请输入采购数量"
                  type="number"
                  :min="specsinfo.min_num"
                  v-model="btcount"
                  oninput="if(value>99999)value=99999"
                />
              </li>
              <li class="num1">
                <div class="imgsa">
                  <img
                    src="@/assets/my/plus.png"
                    @click.stop="add()"
                    class="subaddbt"
                  />
                </div>
              </li>
            </ul>

            <!--cristo-->
          </div>
        </div>
        <div class="yixuan">
          <div class="span1">
            已选规格：
            <span class="span2"
              >{{ specs.length === 0 ? shopinfo.title : specsinfo.name }}
              {{ btcount === "" ? 0 : btcount }}{{ shopinfo.unit }}</span
            >
          </div>
        </div>
        <div class="zong">
          <div class="left">
            <span class="p9">
              总计
              <span class="p8">
                ￥
                <span class="p7">{{ allprice }}</span
                ><!--{{allprice}}-->
              </span>
            </span>
          </div>
          <div class="right" @click="addCard">加入购物车</div>
          <!-- <div class="right" @click="addorder">确定采购</div> -->
        </div>
      </div>
    </div>
    <div class="zhezhao" v-show="isshow"></div>
    <div class="zhezhao1" v-show="isshow">
      <div class="tankuang">
        <div class="guanb" @click="isshow = false">
          <img src="@/assets/pblic/guanbi.png" alt />
        </div>
        <div class="p">您还没有达到最低销售额,不可再次下单</div>
        <div class="btn">
          <div
            class="btn1"
            @click="
              isshow = false;
              isguige = true;
            "
          >
            取消
          </div>
          <div class="btn2" @click="turn">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import MySwiper from "@/components/MySwiper";
import axios from "axios";
import bus from "@/utils/bus";
import { ImagePreview } from "vant";
export default {
  data() {
    return {
      isguige: false, // 选规格
      shopinfo: {},
      shopcontent: "",
      specs: [],
      active: 1,
      specsinfo: {},
      num: 1,
      allprice: 0,
      id: 0,
      // order_id:0
      token: "",
      btcount: "",
      isshow: false,
    };
  },
  components: {
    MySwiper,
  },
  watch: {
    btcount: function (newVal, oldVal) {
      if (this.specsinfo.o_price) {
        if (this.specs.length === 0) {
          this.allprice = newVal * this.shopinfo.price;
        } else {
          this.allprice = newVal * this.specsinfo.o_price;
        }
      } else {
        this.$toast("请选择商品规格");
      }
    },
  },
  mounted() {
    this.token = window.localStorage.getItem("token");
    this.id = this.$route.params.shopid;
    axios
      .post(
        "/index/goods/info",
        this.$qs.stringify({
          id: this.$route.params.shopid,
          token: this.token,
        })
      )
      .then((res) => {
        console.log(res.data, 1111);
        if (res.data.code === 1) {
          this.shopinfo = res.data.data;
          if (res.data.data.content) {
            this.shopcontent = res.data.data.content;
          }
          this.specs = res.data.data.specs;
        } else {
          this.$toast(res.data.msg);
        }
      });
  },
  methods: {
    tupianyulan() {
      ImagePreview(this.shopinfo.images);
    },
    goback() {
      this.$router.back();
    },
    guanbi() {
      this.isguige = false;
    },
    guigecaigou() {
      this.isguige = true;
    },

    add: function (btcount) {
      if (this.specsinfo.o_price) {
        this.btcount++;
      } else {
        this.$toast("请选择商品规格");
      }
    },
    subtract: function (btcount) {
      if (this.btcount <= 1) {
        this.$toast("采购数量无法再减少");
        // this.btcount=1;
      } else {
        this.btcount -= 1;
      }
    },

    change(e) {
      // var val = e.target.value;
      // var btcount = this.btcount * 1;
      // this.num = val + this.btcount;
      if (this.specsinfo.o_price) {
        // this.btcount = e.target.value;
        // if (this.specs.length === 0) {
        //   this.allprice = this.btcount * this.shopinfo.price;
        // } else {
        //   this.allprice = this.btcount * this.specsinfo.o_price;
        // }
      } else {
        this.$toast("请选择商品规格");
      }
    },
    xuanguige(id) {
      this.active = id;
      var specs = this.specs;
      this.specsinfo = specs.filter((item) => item.id === id)[0];
      this.btcount = this.btcount === "" ? 1 : this.btcount;
      if (this.specs.length === 0) {
        this.allprice = this.btcount * this.shopinfo.price;
      } else {
        this.allprice = this.btcount * this.specsinfo.o_price;
      }
    },
    turn() {
      this.isshow = false;
    },
    addCard() {
      if (this.specs.length === 0) {
        if (this.num === 0) {
          this.$toast("商品数量不能为0");
        } else {
          axios
            .post(
              "/index/order/add",
              this.$qs.stringify({
                token: this.token,
                goods_id: this.id,
                price: this.shopinfo.price,
                num: this.btcount,
              })
            )
            .then((res) => {
              if (res.data.code === 1) {
                this.isguige = false;
                this.$toast("加入购物车成功");
              } else if (res.data.code === -10) {
                this.isshow = true;
                this.isguige = false;
              } else if (res.data.code === -1) {
                this.$toast("未登录");
                this.$router.replace("/home");
              } else {
                this.$toast(res.data.msg);
              }
            });
        }
      } else {
        if (
          Object.keys(this.specsinfo).length == 0 ||
          this.btcount === 0 ||
          this.btcount == ""
        ) {
          this.$toast("请选择商品规格并填写数量");
        } else {
          axios
            .post(
              "/index/Cart/goodsOperate",
              this.$qs.stringify({
                token: this.token,
                goods_id: this.id,
                num: this.btcount,
                specs_id: this.active,
              })
            )
            .then((res) => {
              if (res.data.code === 1) {
                this.isguige = false;
                this.$toast("加入购物车成功");
              } else if (res.data.code === -10) {
                this.isshow = true;
                this.isguige = false;
              } else if (res.data.code === -1) {
                this.$toast("未登录");
                this.$router.replace("/home");
              } else {
                this.$toast(res.data.msg);
              }
            });
        }
      }
    },
    addorder() {
      if (this.specs.length === 0) {
        if (this.num === 0) {
          this.$toast("商品数量不能为0");
        } else {
          axios
            .post(
              "/index/order/add",
              this.$qs.stringify({
                token: this.token,
                goods_id: this.id,
                price: this.shopinfo.price,
                num: this.btcount,
              })
            )
            .then((res) => {
              if (res.data.code === 1) {
                this.$router.push(
                  "/confirmorder?order_id=" + res.data.data.order_id
                );
              } else {
                this.$toast(res.data.msg);
              }
            });
        }
      } else {
        if (
          Object.keys(this.specsinfo).length == 0 ||
          this.btcount === 0 ||
          this.btcount == ""
        ) {
          this.$toast("请选择商品规格并填写数量");
        } else {
          axios
            .post(
              "/index/order/add",
              this.$qs.stringify({
                token: this.token,
                goods_id: this.id,
                price: this.specsinfo.o_price,
                num: this.btcount,
                specs_id: this.active,
              })
            )
            .then((res) => {
              if (res.data.code === 1) {
                this.$router.push(
                  "/confirmorder?order_id=" + res.data.data.order_id
                );
              } else if (res.data.code === -10) {
                this.isshow = true;
                this.isguige = false;
              } else if (res.data.code === -1) {
                this.$toast("未登录");
                this.$router.replace("/home");
              } else {
                this.$toast(res.data.msg);
              }
            });
        }
      }
    },
  },
};
</script>
<style>
.xqqqqqq {
  width: 92%;
  overflow: hidden;
  margin: 0 auto;
}
.xqqqqqq p {
  max-width: 100%;
}
.xqqqqqq img {
  max-width: 100%;
}
</style>
<style lang="scss" scoped>
.shopinfo {
  width: 100%;
  height: 100%;
  .slot {
    width: 100%;
    height: 44px;
    .top {
      width: 100%;
      height: 44px;
      display: flex;
      display: -webkit-flex;
      justify-content: center;
      align-items: center;
      background: white;
      position: fixed;
      top: 0;
      left: 0;
      .return {
        width: 38px;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        display: -webkit-flex;
        justify-content: center;
        align-items: center;
        img {
          width: 8px;
          height: 15px;
        }
      }
      span {
        font-size: 18px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #000000;
        line-height: 44px;
      }
    }
  }
  .swiper {
    width: 100%;
    height: 275px;
    .swiper-slide {
      width: 100%;
      height: 100%;
      img {
        display: flex;
        display: -webkit-flex;
        width: 100%;
        height: 275px;
      }
    }
  }
  .info {
    width: 345px;
    height: 55px;
    padding: 15px;
    .name {
      font-size: 19px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #000000;
      line-height: 19px;
    }
    .more {
      display: flex;
      display: -webkit-flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 18px;
      .price {
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #ff511e;
        line-height: 12px;
        .span2 {
          font-size: 34px;
          font-family: PingFang SC;
          font-weight: bold;
          line-height: 34px;
        }
      }
      .num {
        font-size: 12px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #999999;
        line-height: 12px;
      }
    }
  }
  .xiangq {
    width: 100%;
    height: 42.5px;
    background: #f8f8f8;
    font-size: 13px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #999999;
    line-height: 42.5px;
    text-align: center;
  }

  .bottom {
    width: 100%;
    height: 64px;
    background: white;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 10;
    display: flex;
    display: -webkit-flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0px -1px 0px 0px rgba(0, 0, 0, 0.1);
    .yixuan {
      width: 150px;
      font-size: 12px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
      line-height: 12px;
      margin-left: 15px;
      .span1 {
        color: #222222;
      }
    }
    .btn {
      width: 120px;
      height: 44px;
      background: #fdd40a;
      border-radius: 22px;
      margin-right: 15px;
      margin-left: 15px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #222222;
      line-height: 44px;
      text-align: center;
    }
    .btn1 {
      width: 44px;
      height: 44px;
      background: rgb(207, 207, 207);
      border-radius: 22px;
      position: relative;
      margin-left: 15px;
      img {
        width: 50%;
        height: 50%;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }
  .zhezhao {
    width: 100%;
    height: 100%;
    background: #000000;
    opacity: 0.5;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 19;
  }
  .zhezhao2 {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 900;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    align-items: center;
    .tankuang2 {
      width: 330px;
      background: #ffffff;
      border-radius: 10px;
      .top {
        width: 100%;
        height: 49px;
        position: relative;
        text-align: center;
        .name {
          font-size: 18px;
          font-family: PingFang SC;
          font-weight: bold;
          color: #222222;
          line-height: 49px;
        }
        .guanbi {
          width: 18.5px;
          height: 18.5px;
          position: absolute;
          top: 5px;
          right: 5px;
          img {
            width: 18.5px;
            height: 18.5px;
          }
        }
      }
      .guige {
        .p4 {
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          margin-left: 15px;
        }
        .ggbtn {
          display: flex;
          display: -webkit-flex;
          align-items: center;
          flex-wrap: wrap;
          margin-bottom: 20px;
          .btn1 {
            width: 90px;
            height: 37px;
            background: #fdd40a;
            border-radius: 18.5px;
            margin-left: 15px;
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #222222;
            line-height: 37px;
            text-align: center;
            margin-top: 10px;
            overflow: hidden;
          }
          .btn2 {
            width: 90px;
            height: 37px;
            background: #ececec;
            border-radius: 18.5px;
            margin-left: 15px;
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #555555;
            line-height: 37px;
            text-align: center;
            margin-top: 10px;
            overflow: hidden;
          }
        }
        .num {
          width: 300px;
          margin: auto;
          display: flex;
          display: -webkit-flex;
          justify-content: center;
          align-items: center;
          padding: 10px 0;
          border-bottom: 1px solid #d3d3d3;
          .input {
            width: 100%;
            border: none;
            font-size: 15px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #333333;
            line-height: 20px;
            padding: 0 10px;
            box-sizing: border-box;
          }
          .shuliangtianxie {
            .num1 {
              list-style: none;
              float: left;
              text-align: center;
              .input {
                width: 100%;
                height: 100%;
                border: none;
                font-size: 15px;
                font-family: PingFang SC;
                font-weight: 500;
                color: #333333;
                line-height: 20px;
                padding: 0 10px;
              }
              .imgsa {
                width: 26px;
                height: 26px;
              }
              .subaddbt {
                width: 100%;
                height: 100%;
              }
            }
          }
        }
      }
      .yixuan {
        width: 327px;
        height: 33px;
        margin: 22.5px 0 0 0;
        background: #f9f9f9;
        border: 1px solid #e5e5e5;
        .span1 {
          font-size: 12px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #888888;
          line-height: 33px;
          margin-left: 15px;
          .span2 {
            color: #222222;
          }
        }
      }
      .zong {
        width: 300px;
        margin: 15px;
        display: flex;
        display: -webkit-flex;
        justify-content: space-between;
        align-items: center;
        .left {
          .p9 {
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: bold;
            color: #000000;
            line-height: 14px;
            .p8 {
              font-size: 12px;
              font-family: PingFang SC;
              font-weight: bold;
              color: #ff511e;
              line-height: 12px;
              .p7 {
                font-size: 18px;
                font-family: PingFang SC;
                font-weight: bold;
                color: #ff511e;
                line-height: 18px;
              }
            }
          }
        }
        .right {
          width: 90px;
          height: 40px;
          background: #fdd40a;
          border-radius: 20px;
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: bold;
          color: #222222;
          line-height: 40px;
          text-align: center;
        }
      }
    }
  }
}
.zhezhao {
  width: 100%;
  height: 100%;
  background: #000000;
  opacity: 0.5;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 950;
}
.zhezhao1 {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 950;
  display: flex;
  display: -webkit-flex;
  justify-content: center;
  align-items: center;
  .tankuang {
    width: 310px;
    height: 145px;
    background: #ffffff;
    border-radius: 10px;
    position: relative;
    .guanb {
      position: absolute;
      top: 5px;
      right: 5px;
      img {
        width: 18px;
        height: 18px;
      }
    }
    .p {
      width: 100%;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      line-height: 16px;
      text-align: center;
      margin-top: 43px;
    }
    .btn {
      width: 100%;
      display: flex;
      display: -webkit-flex;
      align-items: center;
      justify-content: center;
      margin-top: 30px;
      .btn1 {
        width: 123px;
        height: 37px;
        background: #dddddd;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #555555;
        line-height: 37px;
        text-align: center;
      }
      .btn2 {
        width: 123px;
        height: 37px;
        background: #fdd40a;
        border-radius: 18.5px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 37px;
        text-align: center;
        margin-left: 24px;
      }
    }
  }
}
</style>
